﻿
@{
    Layout = "~/Views/Shared/_LayoutDialog.cshtml";
}
@model OL.FrameCore.Application.User.Dto.GetUserResponse

@section CSS{
    <style>

    </style>
}

<form class="layui-form">
    <div class="layui-tab-content page-detail">
        <div class="layui-tab-item layui-show">
            <input type="hidden" id="hidId" value="@Model.Id" />
            <div class="layui-row">
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名<span class="required">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="tbUserName" name="tbUserName" required lay-verify="required|maxLength20" placeholder="" autocomplete="off" class="layui-input" value="@Model.UserName">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">姓名<span class="required">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="tbTrueName" name="tbTrueName" required lay-verify="required|maxLength20" placeholder="" autocomplete="off" class="layui-input" value="@Model.TrueName">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别<span class="required">*</span></label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="0" title="男" @(Model.Sex == 0 ? "checked" : "")>
                            <input type="radio" name="sex" value="1" title="女" @(Model.Sex == 1 ? "checked" : "")>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话<span class="required">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="tbPhone" required lay-verify="required|maxLength20" placeholder="" autocomplete="off" class="layui-input" value="@Model.Phone">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">Email<span class="required">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="tbEmail" required lay-verify="required|maxLength200" placeholder="" autocomplete="off" class="layui-input" value="@Model.Email">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">部门<span class="required">*</span></label>
                        <div class="layui-input-block">
                            <select id="selectDept" lay-verify="required" lay-search="">
                                <option value="">请选择部门</option>
                                @if (ViewBag.DeptList != null)
                                {
                                    foreach (var li in ViewBag.DeptList)
                                    {
                                        if (li.Id == Model.DeptId)
                                        {
                                            <option value="@li.Id" selected>@li.Name</option>
                                        }
                                        else
                                        {
                                            <option value="@li.Id">@li.Name</option>
                                        }
                                    }
                                }
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-row detail-submit">
                <div class="layui-layer-btn layui-layer-btn-c">
                    <a class="layui-layer-btn0" id="btnSave" lay-submit lay-filter="*">保存</a>
                    <a class="layui-layer-btn0" id="btnCanel" onclick="UserAddOrUpdate.cancel()">取消</a>
                </div>
            </div>
        </div>
    </div>
</form>

@section Scripts{
    <script>
        var UserAddOrUpdate = function () {
            let inited = false;
            let data = {};

            function initData() {
                layui.use(['form', 'layer'], function () {
                    var form = layui.form;
                    form.verify({
                        maxLength20: [/^.{0,20}$/, '长度最大为20'],
                        maxLength200: [/^.{0,200}$/, '长度最大为200']
                    });

                    form.on('submit(*)', function (data) {
                        var id = $("#hidId").val();
                        var userName = $("#tbUserName").val();
                        var trueName = $("#tbTrueName").val();
                        var sex = $("input[name='sex']:checked").val();
                        var phone = $("#tbPhone").val();
                        var email = $("#tbEmail").val();
                        var deptId = $("#selectDept").val();

                        //开启loading
                        layer = layui.layer;
                        var index = layer.load(2);
                        $.ajax({
                            url: "/User/PostUpdateUser",
                            type: "post",
                            data: { id: id, userName: userName, trueName: trueName, sex: sex, phone: phone, email: email, deptId: deptId },
                            success: function (data) {
                                layer.close(index)
                                if (data.state == 1) {
                                    parent.layer.msg("修改成功", { shade: 0.1, icon: 1 }, function () {
                                        //parent.location.reload();
                                        //刷新列表
                                        parent.layui.table.reload("table-list")

                                        var index = parent.layer.getFrameIndex(window.name);
                                        parent.layer.close(index);
                                    });
                                } else if (data.state == 0) {
                                    parent.layer.open({
                                        title: '提示'
                                        , content: "修改失败 " + data.msg
                                        , shade: 0.4
                                        , icon: 5
                                    });
                                } else {
                                    parent.layer.open({
                                        title: '提示'
                                        , content: "修改异常 " + data.msg
                                        , shade: 0.4
                                        , icon: 2
                                    });
                                }
                            }
                        });
                    });


                });
            }

            return {
                init() {//初始化
                    if (!inited) {
                        initData();
                        inited = true;
                    }
                },
                cancel() {//取消
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            };
        }();
        UserAddOrUpdate.init();
    </script>
}
